<?php
class packageControl extends Control{
    //封面显示
    function index(){
        $a[0]['html_file']="h/a.html";
        $this->html_create('package', 'pack_list', $a);exit;
        $packcate=M('packcate');
        if($indextpl=$packcate->field("indextpl")->find("catid={$_GET['catid']}")){
            if($indextpl['indextpl']!=''){
                $this->display($indextpl['indextpl']);exit();
            }            
        }             
        go(__WEB__);
    }
    //游戏列表显示
    function pack_list(){
        $pc=M('packcate');
        $pv=K('packageview');
        $tables="packcate|appcate";
        $sort=array('addtime','hot','good','star');//排序的字段
        $urlarr=array(1=>'game',2=>'soft',3=>'netgame');//地址栏目名称
        //判断info开始
        $_GET['site']=  isset($_GET['aid'])?(int)$_GET['aid']:0;
        $info=explode('_', isset($_GET['info'])?$_GET['info']:0);
        $_GET['leixing']=  isset($info[0])?(int)$info[0]:0;
        $_GET['paixu']=  isset($info[1])?$info[1]:'0';
        $_GET['xitong']=  isset($info[2])?(int)$info[2]:0;
        $_GET['chicun']=  isset($info[3])?(int)$info[3]:0;
        $pv->where("d_package.aid={$_GET['site']}");
        if($_GET['leixing']!=0){$pv->where("d_packcate.cid={$_GET['leixing']}");}
        if($_GET['xitong']!=0){$pv->where("d_version.vid={$_GET['xitong']}");$tables.="|package_version|version";}
        if($_GET['chicun']!=0){$pv->where("d_size.sid={$_GET['chicun']}");$tables.="|package_size|size";}
        $page=new page($pv->join($tables)->count(), 15, 5, 4);
        $pv->where("d_package.aid={$_GET['site']}");
        if($_GET['leixing']!=0){$pv->where("d_packcate.cid={$_GET['leixing']}");}
        if($_GET['xitong']!=0){$pv->where("d_version.vid={$_GET['xitong']}");}
        if($_GET['chicun']!=0){$pv->where("d_size.sid={$_GET['chicun']}");}
        if(in_array($_GET['paixu'], $sort)){
            $row=$pv->join($tables)->limit($page->limit())->order(" d_package.{$_GET['paixu']} desc ")->all();
        }else{
            $row=$pv->join($tables)->limit($page->limit())->all();
        }  
        //判断info结束
        $packlist=$pc->field('cid,cname')->all("pid={$_GET['site']}");        
        $verlist=$pc->table('version')->field('vid,vname')->all();
        $sizelist=$pc->table('size')->field('sid,size')->all();
        $this->assign("result", $row);
        $this->assign("packlist", $packlist);
        $this->assign("verlist", $verlist);
        $this->assign("sizelist", $sizelist);
        if(isset($urlarr[$_GET['site']])){$this->assign("urlname", $urlarr[$_GET['site']]);}else{$this->assign("urlname", '#');}        
        $this->assign("pageshow", $page->show());
        $this->display('plist.html');
    }
    //内容页显示
    public function content(){
        $package=K('packageview');
        $_GET['site']=  isset($_GET['aid'])?(int)$_GET['aid']:0;
        $result=$package->join("good")->find("d_package.id={$_GET['id']}");
        $imgs=  explode('@@', $result['screenshot']);
        $this->assign("result", $result);
        $this->assign("imgs", $imgs);
        $this->display('pcon.html');   
    }
    //生成静态
    function htmltpl(){
        $db=M('package');
        $row=$db->all();
        $a=array();
        foreach($row as $k=>$v){
            $a[$k]['id']=$v['id'];
            $a[$k]['aid']=$v['aid'];
            $a[$k]['html_file']="h/a/".$v['id'].".html";
        }
        $this->html_create('package', 'content', $a);exit;
//        go(__WEB__.'/game');
    }
    //添加评论
    function addcomment(){
        if(!$this->session_is_set('uid')||!isset($_POST['con']))exit;
        $_POST['uid']=  $this->session_get('uid');
        $_POST['checked']=0;
        $db=K('leaveview');
        if($db->add()){
            $row=$db->field("star")->all("id={$_POST['id']}");
            foreach ($row as $v) {
                $total+=$v['star'];
            }
            $total=$total/count($row);
            $package=M('package');
            $arr=array();
            $arr['id']=$_POST['id'];
            $arr['star']=$total;
            $package->update($arr);
            $data=$package->field("star")->find("id={$_POST['id']}");
            $data['leave']=$db->all("id={$_POST['id']}");
            echo json_encode($data);
        }  else {
            exit;
        }
    }
    //好评度
    function good(){
        if(!isset($_POST['id'])||!isset($_POST['isgood']))exit;
        $package=M('package');
        $goodip=M("good_ip");
        $_POST['leavetime']=time();
        $_POST['ip']= ip_get_client();
        $time=$_POST['leavetime']-10;
        if(!$goodip->find("id={$_POST['id']}","and","ip='{$_POST['ip']}'","and","leavetime>{$time}")){
            //echo json_encode($_POST);exit;
            $package->inc("{$_POST['isgood']}","id={$_POST['id']}",1);
            $goodip->add();
            if(rand(1, 6)==6){$goodip->del("leavetime<{$time}");}
        }
        $result=$package->field("good,bad")->find("id={$_POST['id']}");
        echo json_encode($result);
    }
    //显示静态页面
    function showhtml(){
        if(is_file(C('htmldir').(int)$_GET['filename'].".html")){
           include C('htmldir').(int)$_GET['filename'].".html"; 
        }  else {
            echo "没有此网页";
        }
    }
    //读取数据
    function data(){
        if(!isset($_POST['id']))exit;
        $db=M('package');
        $leave=K('leaveview');
        $result=$db->field("star,good,bad")->find("id={$_POST['id']}");
        $result['leave']=$leave->all("id={$_POST['id']}");
        echo json_encode($result);
    }
    function download(){
        $fileUrl = 'abc.zip';
        $fileName = '保存时显示的文件名.zip';
        $data = file_get_contents($fileUrl);
        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length: ".filesize($fileUrl));
        header("Content-Disposition: attachment; filename=" . $fileName);
        echo $data;
        exit;
    }
}
?>
